﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.IO;


namespace wdwp.Desktop.widgetLib.CommonTypes
{

    /// <summary>
    /// 직렬화를 담당하는 클래스
    /// </summary>
    public class Serializer
    {

        /// <summary>
        /// 이진 포매터를 이용한 역직렬화
        /// 이진파일을 객체로 전환해줌
        /// </summary>
        /// <param name="strBinFile">대상파일</param>
        /// <param name="oTargetObj">대상객체</param>
        public static void DeSerialize(string strBinFile, ref object oTargetObj)
        {
            try
            {
                FileStream fStream = File.OpenRead(strBinFile);
                BinaryFormatter BinFormatter = new BinaryFormatter();
                oTargetObj = BinFormatter.Deserialize(fStream);
                fStream.Close();
            }
            catch (Exception exc)
            {
                throw exc;
            }                
        }


        /// <summary>
        /// XML포매터를 이용한 역 직렬화 
        /// XML파일을 객체로 전환해줌
        /// </summary>
        /// <param name="strXmlFile">대상파일</param>
        /// <param name="oTargetObj">목적객체</param>
        /// <param name="objType">변환된 객체의 타입</param>
        /// <param name="strClassName">변환된 객체의 클래스명</param>
        public static void DeSerialize(string strXmlFile, ref object oTargetObj, Type objType, string strClassName)
        {
            try
            {
                FileStream fStream = File.OpenRead(strXmlFile);
                XmlSerializer XmlFormatter = new XmlSerializer(objType, strClassName);
                oTargetObj = XmlFormatter.Deserialize(fStream);
                fStream.Close();
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }


        /// <summary>
        /// 이진 직렬화 - 객체를 파일로 직렬화함
        /// </summary>
        /// <param name="strBinFile"></param>
        /// <param name="oSourceObj"></param>
        public static void Serialize(string strBinFile, object oSourceObj)
        {
            try
            {
                FileStream fStream = File.Create(strBinFile);
                BinaryFormatter BinFormatter = new BinaryFormatter();
                BinFormatter.Serialize(fStream, oSourceObj);
                fStream.Close();
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }


        public static void Serialize(string strXmlfile, object oSourceObj, Type objType, string strClassName)
        {
            try
            {
                FileStream fStream = File.Create(strXmlfile);
                XmlSerializer XmlFormatter = new XmlSerializer(objType, strClassName);
                XmlFormatter.Serialize(fStream, oSourceObj);
                fStream.Close();
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        
    }
}
